import logging

from agent.feature.base import Feature
from agent.common.system import sysCommand
from agent.common.netinfo import NetInfo

logger = logging.getLogger('common')

class RFS(Feature):
    __domain__  = "net"
    __feature__ = "RFS"
    
    def __init__(self):
        super().__init__(self.__domain__, self.__feature__)
        self.net_info = NetInfo()
        assert self.net_info.rx_queue.__len__() != 0
        
    def set_value(self, value):
        if type(value) == str and value == "on":
            for queue_id in self.net_info.rx_queue:
                sysCommand("echo 4096 > /sys/class/net/{}/queues/{}/rps_flow_cnt".format(
                    self.net_info.dev_name,
                    queue_id
                ))
        else:
            for queue_id, value in value.items():
                sysCommand("echo {} > /sys/class/net/{}/queues/{}/rps_flow_cnt".format(
                    value,
                    self.net_info.dev_name,
                    queue_id
                ))
                
    def get_value(self):
        rfs_binding_schema = {}
        for queue_id in self.net_info.rx_queue:
            rps_flow_cnt = sysCommand("cat /sys/class/net/{}/queues/{}/rps_flow_cnt".format(
                self.net_info.dev_name,
                queue_id
            ))
            rfs_binding_schema[queue_id] = rps_flow_cnt
        
        return rfs_binding_schema

if __name__ == "__main__":
    rfs = RFS()
    rfs.backup()
    rfs.set_value("on")
    rfs.rollback()